package com.dengqi.mymodule.activity.unittest;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;

/**
 * Created by dell on 2019/2/21
 */
public class SharedPreferencesHelperTest {

    private Context mContext;
    private SharedPreferences mSharePreferences;
    private SharedPreferencesHelper sharedPreferencesHelper;

    @Before
    public void setUp() throws Exception {
        //获取application的context
        mContext = InstrumentationRegistry.getTargetContext();
        //实例化SharedPreferences
        mSharePreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        sharedPreferencesHelper = new SharedPreferencesHelper(mContext);
    }

    @Test
    public void saveValue() {
        String value = "hehe";
        sharedPreferencesHelper.saveValue("key", value);

        String actual = mSharePreferences.getString("key", "");
        TestCase.assertEquals(value, actual);
    }

    @Test
    public void loadValue() {
        String value = "haha";
        mSharePreferences.edit().putString("key", value).apply();

        String actual = sharedPreferencesHelper.loadValue("key");
        TestCase.assertEquals(value, actual);

    }

}